/**
 * File: FeatureVector.cpp
 * Date: November 2011
 * Author: Dorian Galvez-Lopez
 * Description: feature vector
 * License: see the LICENSE.txt file
 */

#include <map>
#include <vector>
#include <iostream>

#include "DBoW2/FeatureVector.h"

namespace DBoW2 {

// ---------------------------------------------------------------------------

FeatureVector::FeatureVector() {}

// ---------------------------------------------------------------------------

FeatureVector::~FeatureVector() {}

// ---------------------------------------------------------------------------

void FeatureVector::addFeature(const NodeId id, const unsigned int i_feature) {
  FeatureVector::iterator vit = this->lower_bound(id);

  if (vit != this->end() && vit->first == id) {
    vit->second.push_back(i_feature);
  }
  else {
    vit = this->insert(vit, FeatureVector::value_type(id,
                                                      std::vector<unsigned int>()));
    vit->second.push_back(i_feature);
  }
}

// ---------------------------------------------------------------------------

std::ostream& operator<<(std::ostream& out,
                         const FeatureVector& v) {
  if (!v.empty()) {
    FeatureVector::const_iterator vit = v.begin();

    const std::vector<unsigned int>* f = &vit->second;

    out << "<" << vit->first << ": [";
    if (!f->empty())
      out << (*f)[0];
    for (unsigned int i = 1; i < f->size(); ++i) {
      out << ", " << (*f)[i];
    }
    out << "]>";

    for (++vit; vit != v.end(); ++vit) {
      f = &vit->second;

      out << ", <" << vit->first << ": [";
      if (!f->empty())
        out << (*f)[0];
      for (unsigned int i = 1; i < f->size(); ++i) {
        out << ", " << (*f)[i];
      }
      out << "]>";
    }
  }

  return out;
}

// ---------------------------------------------------------------------------

} // namespace DBoW2
